#include "MutaGraphicsView.h"
#include <QResizeEvent>
#include <QDebug>
#include <QtOpenGL/QtOpenGL>
#include "MutaScene.h"
#include "MutaEvent.h"
#include "MutaLine.h"
#include "MutaCurve.h"
#include  <QPalette>
#include <math.h>
#include <QWriteLocker>
#include "MutaUserCursor.h"


MutaGraphicsView::MutaGraphicsView( QWidget* parent,MutaScene* scene,MutaCursor *cursor )
    : QGraphicsView( parent ),
      editMode(Muta::Event),
      totalScaleFactor(1),
      cursor(cursor),
      scrollable(true),
      layer(0),
      beats(4),
      commandDown(false)
{
    setScene(scene);
    init();
    setMouseTracking(true);
}

MutaGraphicsView::MutaGraphicsView()
{
    init();
}

MutaGraphicsView::~MutaGraphicsView()
{
    //delete selectionRect;
}

void MutaGraphicsView::init()
{
    //setMouseTracking(true);
    setWindowTitle( "Engine Main Window" );
    viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
    setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
    QGLFormat glFormat( QGL::SampleBuffers );
    setRenderHints( QPainter::HighQualityAntialiasing | QPainter::SmoothPixmapTransform );
    setViewport( new QGLWidget( glFormat ) ); // Set Anti-Aliasing and OpenGL
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    verticalScrollBar()->setStyle(new QCleanlooksStyle());
    QPalette scrollBarPallete;
    scrollBarPallete.setColor(QPalette::Background,QColor(75,155,255,255));
    scrollBarPallete.setColor(QPalette::Button,QColor(180,180,180,255));
    scrollBarPallete.setColor(QPalette::Window,QColor(180,180,180,255));
    verticalScrollBar()->setPalette(scrollBarPallete);
    horizontalScrollBar()->setStyle(new QCleanlooksStyle());
    horizontalScrollBar()->setPalette(scrollBarPallete);
    this->setStyle(new QCleanlooksStyle());
    show();

    selectionRect = new QGraphicsRectItem( QRect( 0, 0, 0, 0 ) );
    selectionRect->setPen( QColor( 255, 255, 255, 200 ) );
    selectionRect->setBrush( QColor( 255, 255, 255, 30 ) );
    //selectionRect->setZValue( 10000 );
    scene()->addItem( selectionRect );
    selectionRect->hide();

    scene()->setSceneRect(QRect(0,0,12000,15*127));
    setTransform(QTransform().scale(1,1));
    centerOn(QPoint(0,0));
    QColor fillColor = QColor(0,0,0,255);
    QBrush brush;
    brush.setColor(fillColor);
    brush.setStyle(Qt::SolidPattern);
    setBackgroundBrush(brush);
}

void MutaGraphicsView::resizeEvent( QResizeEvent *event )
{
    if( scene() )
    QGraphicsView::resizeEvent( event );
}

void MutaGraphicsView::keyPressEvent( QKeyEvent *event )
{
    if(event->matches(QKeySequence::ZoomIn) || event->key()== 61) {
        totalScaleFactor+=0.125;
        setTransform(QTransform().scale(totalScaleFactor,totalScaleFactor));
        centerOn(QCursor::pos());
        emit zoomChanged(totalScaleFactor);
    } else if(event->matches(QKeySequence::ZoomOut)) {
        totalScaleFactor-=0.125;
        setTransform(QTransform().scale(totalScaleFactor,totalScaleFactor));
        centerOn(QCursor::pos());
        emit zoomChanged(totalScaleFactor);
    }else if( event->matches( QKeySequence::Cut ) || event->matches( QKeySequence::Copy ) ) {
        /*
        QList<QGraphicsItem*> items = this->scene()->selectedItems();
        copyItems.clear();
        for( int i = 0; i < items.size(); i++ )
        {
            switch(items.at(i)->type()) {
                case Muta::Event :
                    copyItems.append( new MutaEvent(Muta::getObjectID(),"curtis",lastMouseViewPos.x() - items.at(i)->scenePos().x(),lastMouseViewPos.y() - items.at(i)->scenePos().y(),layer) );
                    break;
            }

        }
        */
    }

    if( event->matches( QKeySequence::Cut ) || event->matches( QKeySequence::Delete ) ) {
        //QList<QGraphicsItem*> items;
        foreach(QGraphicsItem* item, this->scene()->selectedItems())
        {
            if(!(item->type() == Muta::ControlPoint)) {
                if(Muta::ONLINE) {
                    emit removeObject(((MutaEvent*)item)->getObjectID());
                } else {
                    ((MutaScene*)scene())->deleteItem(item);
                }
            }
        }
        //foreach(QGraphicsItem* item,items) {

        //}
    } else if( event->matches( QKeySequence::Paste )) {
        for( int i = 0; i < copyItems.size(); i++ )
        {
            copyItems.at(i)->setPos(copyItems.at(i)->scenePos() + lastMouseViewPos);
            scene()->addItem(copyItems.at(i));
        }
        copyItems.clear();
    } else if(event->modifiers().testFlag(Qt::ControlModifier)) {
        setCursor(Qt::CrossCursor);
        commandDown = true;
    }

    QGraphicsView::keyPressEvent( event );
}

void MutaGraphicsView::mouseMoveEvent( QMouseEvent *event )
{
    if(Muta::ONLINE)
    {
        emit cursorPositionChanged(Muta::userName+"MutaCursor",(QPointF)event->pos());

    }
    lastMouseViewPos = QGraphicsView::mapToScene(event->pos()).toPoint();
    if( event->buttons().testFlag( Qt::RightButton ) && event->buttons().testFlag( Qt::LeftButton ) ) {
        QList<QGraphicsItem*> selectedItems = scene()->selectedItems();
        for(int i=0; i<selectedItems.size();i++) {
            selectedItems.at(i)->setSelected(false);
        }
    } else {

        //if( event->buttons().testFlag( Qt::LeftButton )  && !event->buttons().testFlag( Qt::RightButton ) )
        if( event->buttons().testFlag( Qt::LeftButton )  && !event->modifiers().testFlag(Qt::ControlModifier) )
        {
            // handle selectionRect
            if( selectionRect->isVisible() )
            {
                QRect rubberRect( mouseDownPos, lastMouseViewPos );
                float x1,x2,y1,y2;
                if(mouseDownPos.x() < lastMouseViewPos.x()) {
                    x1 = mouseDownPos.x();
                    x2 = lastMouseViewPos.x();
                } else {
                    x2 = mouseDownPos.x();
                    x1 = lastMouseViewPos.x();
                }
                if(mouseDownPos.y() < lastMouseViewPos.y()) {
                    y1 = mouseDownPos.y();
                    y2 = lastMouseViewPos.y();
                } else {
                    y2 = mouseDownPos.y();
                    y1 = lastMouseViewPos.y();
                }

                selectionRect->setRect(x1,y1,x2 - x1,y2 - y1);
                QPainterPath path;
                path.addRect(x1,y1,x2 - x1,y2 - y1);

                scene()->setSelectionArea( path, Qt::IntersectsItemShape );
            }
            // Always remember to have the parent class handle the event
            QGraphicsView::mouseMoveEvent( event );
            foreach (QGraphicsItem* item, scene()->selectedItems()) {
                item->setPos(item->pos());
            }
        //} else if( event->buttons().testFlag( Qt::RightButton ) ) {
        }

    }

    if(event->modifiers().testFlag(Qt::ControlModifier)) {
                switch(editMode) {
                    case Muta::Event :
                        if(tempEvent) {
                            tempEvent->changeLength(QGraphicsView::mapToScene( event->pos() ).toPoint());
                        }
                        break;
                    case Muta::Line :
                        if(tempLine) {
                            tempLine.data()->setFinalPoint(QGraphicsView::mapToScene( event->pos() ).toPoint());
                        }
                        break;
                    case Muta::Curve :
                        if(tempCurve) {
                            tempCurve.data()->setFinalPoint(QGraphicsView::mapToScene( event->pos() ).toPoint());
                        }
                        break;
                    case Muta::StrangeAttractor :
                        if(tempStrangeAttractor) {
                            tempStrangeAttractor.data()->setFinalPoint(QGraphicsView::mapToScene( event->pos() ).toPoint());
                        }
                        break;
                    case Muta::AutoTendril :
                        if(tempTendril) {
                            tempTendril.data()->setFinalPoint(QGraphicsView::mapToScene( event->pos() ).toPoint());
                        }
                        break;
                }
      }

}
void MutaGraphicsView::keyReleaseEvent(QKeyEvent *event) {

    QGraphicsView::keyReleaseEvent( event );

    if( !event->modifiers().testFlag( Qt::ControlModifier ) ) {
        tempEvent = NULL;
        tempLine = NULL;
        tempCurve = NULL;
        tempTendril = NULL;
        tempStrangeAttractor = NULL;
        //setMouseTracking(false);
        setCursor(Qt::ArrowCursor);
    } else if(event->modifiers().testFlag(Qt::ControlModifier)) {
        commandDown = false;
    }
}

void MutaGraphicsView::mouseReleaseEvent( QMouseEvent *event )
{
    selectionRect->hide();
    foreach(QGraphicsItem* item, this->scene()->selectedItems())
    {
        if(!(item->type() == Muta::ControlPoint)) {
            if(Muta::ONLINE) {
                switch(item->type()) {
                    case Muta::Event:
                        ((MutaEvent*)item)->forceOSCthulhuUpdate();
                        break;
                    case Muta::Line:
                        ((MutaLine*)item)->forceOSCthulhuUpdate();
                        break;
                    case Muta::Curve:
                        ((MutaCurve*)item)->forceOSCthulhuUpdate();
                        break;
                    case Muta::AutoTendril:
                        ((MutaTendril*)item)->forceOSCthulhuUpdate();
                        break;
                case Muta::Repeat:
                        ((MutaRepeat*)item)->forceOSCthulhuUpdate();
                        break;
                }
            }
        }
    }
    // Always remember to have the parent class handle the event
    QGraphicsView::mouseReleaseEvent( event );
}


void MutaGraphicsView::mousePressEvent( QMouseEvent *event )
{
    //if( event->buttons().testFlag( Qt::RightButton ) && event->buttons().testFlag( Qt::LeftButton ) ) {
    if(event->buttons().testFlag(Qt::LeftButton) && event->modifiers().testFlag(Qt::ControlModifier) && event->modifiers().testFlag(Qt::AltModifier)) {
         if(tempLine) {
            tempLine.data()->addControlPoint(QGraphicsView::mapToScene( event->pos() ).toPoint());
         }
         QList<QGraphicsItem*> selectedItems = scene()->selectedItems();
         for(int i=0; i<selectedItems.size();i++) {
             selectedItems.at(i)->setSelected(false);
         }
    } else  {
        // Always remember to have the parent class handle the event
        QGraphicsView::mousePressEvent( event );
        mouseDownPos = QGraphicsView::mapToScene( event->pos() ).toPoint();

        if( !scene()->itemAt( mapToScene( event->pos() ) ) ) {
            lastMouseViewPos = event->pos();
            selectionRect->setRect( mapFromScene( mouseDownPos ).x(), mapFromScene( mouseDownPos ).y(), 0, 0 );
            selectionRect->show();
        }
        //if( event->buttons().testFlag( Qt::RightButton ) ) {
        if(event->buttons().testFlag(Qt::LeftButton) && event->modifiers().testFlag(Qt::ControlModifier)){
            if(tempLine) {
               tempLine.data()->addControlPoint(QGraphicsView::mapToScene( event->pos() ).toPoint());
               QList<QGraphicsItem*> selectedItems = scene()->selectedItems();
               for(int i=0; i<selectedItems.size();i++) {
                   selectedItems.at(i)->setSelected(false);
               }

            } else if(tempCurve) {
                tempCurve.data()->addControlPoint(QGraphicsView::mapToScene( event->pos() ).toPoint());
                QList<QGraphicsItem*> selectedItems = scene()->selectedItems();
                for(int i=0; i<selectedItems.size();i++) {
                    selectedItems.at(i)->setSelected(false);
                }
            } else if(tempStrangeAttractor) {
                tempStrangeAttractor.data()->addControlPoint(QGraphicsView::mapToScene( event->pos() ).toPoint());
                QList<QGraphicsItem*> selectedItems = scene()->selectedItems();
                for(int i=0; i<selectedItems.size();i++) {
                    selectedItems.at(i)->setSelected(false);
                }
            } else if(tempTendril) {
                tempTendril.data()->addControlPoint(QGraphicsView::mapToScene( event->pos() ).toPoint());
                QList<QGraphicsItem*> selectedItems = scene()->selectedItems();
                for(int i=0; i<selectedItems.size();i++) {
                    selectedItems.at(i)->setSelected(false);
                }
            } else {

            switch(editMode) {
                case Muta::Event :
                    if(Muta::ONLINE) {
                        emit addEvent(Muta::quantizeD(mouseDownPos.x(),Muta::BEAT_PIXELS),Muta::quantizeD(mouseDownPos.y(),15),layer,30);
                    } else {
                        tempEvent = new MutaEvent(Muta::getObjectID(),Muta::userName,mouseDownPos.x(),mouseDownPos.y(),layer);
                        scene()->addItem( tempEvent );
                        setMouseTracking(true);
                        setCursor(Qt::CrossCursor);
                    }
                    break;
                case Muta::Line :
                    if(Muta::ONLINE) {
                        emit addLine("l",Muta::quantizeD(mouseDownPos.x(),Muta::BEAT_PIXELS),Muta::quantizeD(mouseDownPos.y(),15),layer);
                    } else {
                        tempLine = new MutaLine(Muta::getObjectID(),Muta::userName,mouseDownPos,QPointF(10,0),layer);
                        QObject::connect(tempLine,SIGNAL(ellipseAdded(QGraphicsItem*)),this,SLOT(addItem(QGraphicsItem*)));
                        scene()->addItem(tempLine);
                        setMouseTracking(true);
                        setCursor(Qt::CrossCursor);
                    }
                    break;
                case Muta::Curve :
                    if(Muta::ONLINE) {
                        emit addLine("c",Muta::quantizeD(mouseDownPos.x(),Muta::BEAT_PIXELS),Muta::quantizeD(mouseDownPos.y(),15),layer);
                    } else {
                        tempCurve = new MutaCurve(Muta::getObjectID(),Muta::userName,mouseDownPos,QPointF(10,0),layer);
                        QObject::connect(tempCurve,SIGNAL(ellipseAdded(QGraphicsItem*)),this,SLOT(addItem(QGraphicsItem*)));
                        scene()->addItem(tempCurve);
                        setMouseTracking(true);
                        setCursor(Qt::CrossCursor);

                    }
                    break;
                case Muta::StrangeAttractor :
                    tempStrangeAttractor = new MutaStrangeAttractor(Muta::userName,mouseDownPos,QPointF(10,0),layer);
                    QObject::connect(tempStrangeAttractor,SIGNAL(ellipseAdded(QGraphicsItem*)),this,SLOT(addItem(QGraphicsItem*)));
                    scene()->addItem(tempStrangeAttractor);
                    setMouseTracking(true);
                    setCursor(Qt::CrossCursor);
                    break;
                case Muta::AutoTendril :
                    tempTendril = new MutaTendril(Muta::userName,mouseDownPos,QPointF(10,0),layer);
                    QObject::connect(tempTendril,SIGNAL(ellipseAdded(QGraphicsItem*)),this,SLOT(addItem(QGraphicsItem*)));
                    scene()->addItem(tempTendril);
                    setMouseTracking(true);
                    setCursor(Qt::CrossCursor);
                    break;
                case Muta::Repeat :
                    tempRepeat = new MutaRepeat(Muta::userName,mouseDownPos.x(),mouseDownPos.y(),layer);
                    scene()->addItem( tempRepeat );
                    emit repeatObjectAdded(tempRepeat);
                    break;
                }
            }
        }
    }
}

void MutaGraphicsView::addItem(QGraphicsItem *item) {
    scene()->addItem(item);
}

void MutaGraphicsView::drawUpdate()
{
    //drawForeground();
}

void MutaGraphicsView::drawBackground(QPainter* painter, const QRectF& rect)
{

        QGraphicsView::drawBackground(painter,rect);
        int gridInterval = 15; //interval to draw grid lines at
        painter->setWorldMatrixEnabled(true);

        qreal left = 0;
        qreal top = 0;

        QColor strongBeat = QColor(0,0,0,255);
        QColor outlineColor = QColor( 50,50,50,255 );        
        QColor yColor1 = QColor(75,75,75,200);
        QColor yColor2 = QColor(150,150,150,200);
        QColor xColor = QColor(0,150,255,255);

        strongBeat = QColor(100,100,100,255);
        yColor2 = QColor(0,0,0,255);
        yColor1 = QColor(30,30,30,255);
        //xColor = QColor(0,0,0,255);

        QPen pen;
        pen.setColor(outlineColor);
        painter->setPen(pen);

        QBrush yBrush1,yBrush2,xBrush;
        yBrush1.setColor(yColor1);
        yBrush1.setStyle(Qt::SolidPattern);
        yBrush2.setColor(yColor2);
        yBrush2.setStyle(Qt::SolidPattern);
        xBrush.setColor(xColor);
        xBrush.setStyle(Qt::SolidPattern);

        int i=0;
        for (qreal y = top; y < rect.bottom(); y += gridInterval*1 ) {
            if(y>=rect.top()) {
            switch(11-i) {
                case 0:
                case 2:
                case 4:
                case 5:
                case 7:
                case 9:
                case 11:
                    painter->setBrush(yBrush2);
                    //painter->drawRect(QRect(QPoint(rect.left(), y), QPoint(rect.right(), y+gridInterval)));
                    break;
                case 1:
                case 3:
                case 6:
                case 8:
                case 10:
                    painter->setBrush(yBrush1);
                    //painter->drawRect(QRect(QPoint(rect.left(), y), QPoint(rect.right(), y+gridInterval)));
                    break;
            }
            }
            i = (i+1)%12;
        }

        QVarLengthArray<QLineF, 100> linesX;
        QVarLengthArray<QLineF, 100> linesX2;
        //gridInterval *= 2;
        i=0;
        for (int x = left; x < rect.right(); x += Muta::BEAT_PIXELS ) {
            if(x > rect.left()) {
                float top;
                float bottom;
                if(rect.top() > 0) {
                    top = rect.top();
                } else {
                    top = 0;
                }

                if(rect.bottom() < scene()->height()) {
                    bottom = rect.bottom();
                } else {
                    bottom = scene()->height();
                }
                    if(i%beats == 0) {
                        linesX2.append(QLineF(x, top, x, bottom));
                    } else {
                        linesX.append(QLineF(x,top, x, bottom));
                    }
            }
            i++;
        }

        pen.setColor(outlineColor);
        pen.setWidth(1);
        painter->setPen(pen);
        painter->drawLines(linesX.data(), linesX.size());

        pen.setColor(strongBeat);
        pen.setWidth(2);
        painter->setPen(pen);
        painter->drawLines(linesX2.data(), linesX2.size());
        pen.setWidth(10);
        pen.setColor(QColor(40,70,85,255));
        painter->setPen(pen);
        painter->drawLine(QPointF(0,0),QPointF(scene()->width(),0));
        painter->drawLine(QPointF(0,scene()->height()),QPointF(scene()->width(),scene()->height()));
}

void MutaGraphicsView::drawForeground(QPainter* painter, const QRectF& rect)
{
    QGraphicsView::drawForeground(painter,rect);

    QColor outlineColor = QColor( 200,200,200,255 );
    QPen pen;
    pen.setColor(outlineColor);
    pen.setWidth(3);
    painter->setPen(pen);
    painter->drawLine(cursor->x(),0,cursor->x(),scene()->sceneRect().height());

}

void MutaGraphicsView::setEditMode(Muta::MutaType editMode) {
    this->editMode = editMode;
}

void MutaGraphicsView::scrollContentsBy(int dx, int dy)
{
    if(scrollable) {
        emit scrolling(horizontalScrollBar()->value()-dx,verticalScrollBar()->value()-dy);
    }
}
void MutaGraphicsView::multiScrollContentsBy(int dx, int dy)
{
    scrollable = false;
    horizontalScrollBar()->setValue(dx);
    verticalScrollBar()->setValue(dy);
    QGraphicsView::scrollContentsBy(dx,dy);
    scrollable = true;
}

int MutaGraphicsView::getLayer()
{
    return layer;
}

void MutaGraphicsView::setLayer(int layer)
{
    this->layer = layer;
}

void MutaGraphicsView::setBeats(int beats)
{
    this->beats = beats;
    update();
}

void MutaGraphicsView::updateCursor(qreal delta)
{
    viewport()->update();
}


//OSCthulhu SLOTS
void MutaGraphicsView::addMutaEvent(QString objectID, QString owner, float x, float y, int layer, float length)
{
    //tempEvent = new MutaEvent(objectID,owner,mouseDownPos.x(),mouseDownPos.y(),layer);
    //scene()->addItem( tempEvent );
    //setMouseTracking(true);
    //setCursor(Qt::CrossCursor);
    QWriteLocker locker(&mutaObjectsLock);
    MutaEvent* mutaEvent = new MutaEvent(objectID,owner,x,y,layer);
    mutaObjects.insert(objectID,mutaEvent);
    scene()->addItem(mutaEvent);
    emit addedMutaEventToScene(mutaEvent);
}

void MutaGraphicsView::addMutaLine(QString objectID, QString type,QString owner,float x,float y,int layer,QString controlPoints)
{
    QWriteLocker locker(&mutaObjectsLock);
    if(type.compare("l")==0) {
        if(commandDown && ((owner.compare(Muta::userName)) == 0)) {
            commandDown = false;
            tempLine = new MutaLine(objectID,owner,QPointF(x,y),QPointF(64,0),layer);
            QObject::connect(tempLine,SIGNAL(ellipseAdded(QGraphicsItem*)),this,SLOT(addItem(QGraphicsItem*)));
            scene()->addItem(tempLine);
            setMouseTracking(true);
            setCursor(Qt::CrossCursor);
            mutaObjects.insert(objectID,tempLine);
            tempLine->setControlPoints(controlPoints);
            emit addedMutaLineToScene(tempLine);
        } else {

            MutaLine* mutaLine = new MutaLine(objectID,owner,QPointF(x,y),QPointF(64,0),layer);
            mutaObjects.insert(objectID,mutaLine);
            scene()->addItem(mutaLine);
            mutaObjects.insert(objectID,mutaLine);
            mutaLine->setControlPoints(controlPoints);
            emit addedMutaLineToScene(mutaLine);
        }
    } else if(type.compare("c")==0){
        if(commandDown && ((owner.compare(Muta::userName)) == 0)) {
            commandDown = false;
            tempCurve = new MutaCurve(objectID,owner,QPointF(x,y),QPointF(64,0),layer);
            QObject::connect(tempCurve,SIGNAL(ellipseAdded(QGraphicsItem*)),this,SLOT(addItem(QGraphicsItem*)));
            scene()->addItem(tempCurve);
            setMouseTracking(true);
            setCursor(Qt::CrossCursor);
            mutaObjects.insert(objectID,tempCurve);
            tempCurve->setControlPoints(controlPoints);
            emit addedMutaCurveToScene(tempCurve);
        } else {
            MutaCurve* mutaCurve = new MutaCurve(objectID,owner,QPointF(x,y),QPointF(64,0),layer);
            mutaObjects.insert(objectID,mutaCurve);
            scene()->addItem(mutaCurve);
            mutaObjects.insert(objectID,mutaCurve);
            mutaCurve->setControlPoints(controlPoints);
            emit addedMutaCurveToScene(mutaCurve);
        }
    }
}

void MutaGraphicsView::removeMutaObject(QString objectID)
{
    QWriteLocker locker(&mutaObjectsLock);
    scene()->removeItem((QGraphicsItem*)mutaObjects.value(objectID));
    delete mutaObjects.value(objectID);
    mutaObjects.remove(objectID);
}

void MutaGraphicsView::setMutaX(QString objectID, float x)
{
    QReadLocker locker(&mutaObjectsLock);
    if(mutaObjects.contains(objectID)) {
        switch(mutaObjects.value(objectID)->getType()) {
            case Muta::Event:
                if(!scene()->selectedItems().contains((MutaEvent*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetX(x);
                }
                break;
            case Muta::Line:
                if(!scene()->selectedItems().contains((MutaLine*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetX(x);
                }
                break;
            case Muta::Curve:
                if(!scene()->selectedItems().contains((MutaCurve*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetX(x);
                }
                break;
            case Muta::AutoTendril:
                if(!scene()->selectedItems().contains((MutaTendril*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetX(x);
                }
                break;
            case Muta::Repeat:
                if(!scene()->selectedItems().contains((MutaRepeat*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetX(x);
                }
            break;
            case Muta::UserCursor:
                if(!scene()->selectedItems().contains((MutaUserCursor*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetX(x);
                }
            break;
        }
    }
}

void MutaGraphicsView::setMutaY(QString objectID, float y)
{
    QReadLocker locker(&mutaObjectsLock);
    if(mutaObjects.contains(objectID)) {
        switch(mutaObjects.value(objectID)->getType()) {
            case Muta::Event:
                if(!scene()->selectedItems().contains((MutaEvent*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetY(y);
                }
                break;
            case Muta::Line:
                if(!scene()->selectedItems().contains((MutaLine*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetY(y);
                }
                break;
            case Muta::Curve:
                if(!scene()->selectedItems().contains((MutaCurve*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetY(y);
                }
                break;
            case Muta::AutoTendril:
                if(!scene()->selectedItems().contains((MutaTendril*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetY(y);
                }
                break;
            case Muta::Repeat:
                if(!scene()->selectedItems().contains((MutaRepeat*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetY(y);
                }
                break;
            case Muta::UserCursor:
                if(!scene()->selectedItems().contains((MutaUserCursor*)mutaObjects.value(objectID))) {
                    mutaObjects.value(objectID)->remoteSetY(y);
                }
                break;
        }
    }
}

void MutaGraphicsView::setControlPoints(QString objectID, QString controlPointString)
{
    QReadLocker locker(&mutaObjectsLock);

    if(mutaObjects.contains(objectID)) {
       // qDebug() << controlPointString << objectID;
        //switch(mutaObjects.value(objectID)->getType()) {
        //    case Muta::Line:
        //        qDebug() << controlPointString << objectID;
                //if(!scene()->selectedItems().contains((MutaLine*)mutaObjects.value(objectID))) {
                    ((MutaLine*)mutaObjects.value(objectID))->setControlPoints(controlPointString);
                //}
        //        break;
        //}
    }
}

void MutaGraphicsView::userAdded(QString userName)
{

}

void MutaGraphicsView::userRemoved(QString userName)
{

}

void MutaGraphicsView::addUserCursor(QString objectID, QString userName)
{
    if(userName != Muta::userName && (!mutaObjects.contains(userName)))
    //if(!mutaObjects.contains(objectID))
    {
        qDebug() << "Adding userCursor!";
        MutaUserCursor* cursorGraphic = new MutaUserCursor(objectID,userName);
        cursorGraphic->setPos(scene()->sceneRect().bottomRight());
        mutaObjects.insert(objectID,cursorGraphic);
        scene()->addItem(cursorGraphic);
    }
}

/*
void MutaGraphicsView::updateCursorX(QString userName, int xPos)
{
    if(userName != Muta::userName && mutaObjects.contains(userName))
    {
        mutaObjects.value(userName)->setX(xPos);
    }
}

void MutaGraphicsView::updateCursorY(QString userName, int yPos)
{
    if(userName != Muta::userName && mutaObjects.contains(userName))
    {
        mutaObjects.value(userName)->setY(yPos);
    }
}
*/
